<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React!</title>
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<h1>State 状态 生命周期</h1>
<p>componentDidMount--加载，componentWillUnMount--卸载 【实现自动定时器效果】</p>
<p>() => this.tick() 封装匿名方法</p>
<div id="example"></div>
<script type="text/babel">
	class Clock extends React.Component{
		constructor(props) {
		    super(props);
			{/* {} 可能还有保存键值对的功能 */}
			this.state = {date:new Date()};
		}
		
		componentDidMount(){
			this.timerID = setInterval(
				() => this.tick(),
				1000
			);
		}
		
		componentWillUnMount(){
			clearInterval(this.timerID);
		}
		
		tick(){
			this.setState({
				date:new Date()
			});
		}
		
		render(){
			return <h2>当前时间为: {this.state.date.toLocaleString()}</h2> ;
		}
	}
	
	ReactDOM.render(
		<Clock/>,
		document.getElementById('example')
	);
</script>

</body>
</html>